package com.example.zy5_project_jetpack.mvp.view.FindFragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zy5_project_jetpack.Manager.RetrofitManager;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.view.FindFragment.Adapter.ReCommentAdapter;
import com.example.zy5_project_jetpack.mvp.view.FindFragment.Bean.RecommentBean;
import com.google.gson.Gson;

import java.net.URL;
import java.util.List;

public class ReCommentFragment extends Fragment {


    private ReCommentAdapter reCommentAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_re_comment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    private void initData() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String http = RetrofitManager.getInstance().getHttp();
                    Log.e("tag1", http);
                    RecommentBean recommentBean = new Gson().fromJson(http, RecommentBean.class);
                    final List<RecommentBean.DataBeanX.DataBean> data = recommentBean.getData().getData();

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            reCommentAdapter.addReCommentList(data);
                        }
                    });
                    Log.e("tag1", data.get(0).getTitle());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    private void initView(View view) {
        RecyclerView recycler_comment = view.findViewById(R.id.recycler_comment);
        recycler_comment.setLayoutManager(new LinearLayoutManager(getActivity()));
        reCommentAdapter = new ReCommentAdapter(getActivity());
        recycler_comment.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        recycler_comment.setAdapter(reCommentAdapter);

    }
}